<template>
<div class="filContainer">
  <div>
    <el-form :inline="true" ref="add_data">
      <el-date-picker
      v-model="time1"
      type="datetime"
      placeholder="选择开始时间"
      >
    </el-date-picker>
    --
    <el-date-picker
      v-model="time2"
      type="datetime"
      placeholder="选择结束时间"
      >
     </el-date-picker>

       <el-form-item style="margin-left:10px;">
        <el-button type="primary" size="small" icon="view" @click="handleSearch()">筛选</el-button>
      </el-form-item>
      <el-form-item class="btnRight">
        <el-button type="primary" size="small" icon="view" @click="handleAdd()">添加</el-button>
      </el-form-item>
    </el-form>
  </div>
  <div class="table_container">
    <el-table
      v-if="tableData.length > 0"
      :data="tableData"
      max-height="450"
      border
      style="width: 100%"
    >
      <el-table-column prop="date" align="center" label="创建时间" width="210">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.date }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="type" align="center" label="收支类型" width="120"></el-table-column>
      <el-table-column prop="describe" align="center" label="描述" width="180"></el-table-column>
      <el-table-column prop="income" align="center" label="收入" width="80">
          <template slot-scope="scope">
              <span style="color:#00d053">{{ scope.row.income }}</span>
          </template>
      </el-table-column>
      <el-table-column prop="expend" align="center" label="支出" width="100">
          <template slot-scope="scope">
              <span style="color:#f56767">{{ scope.row.expend }}</span>
          </template>
      </el-table-column>
      <el-table-column prop="cash" align="center" label="账户现金" width="100">
          <template slot-scope="scope">
              <span style="color:#4db3ff">{{ scope.row.cash }}</span>
          </template>
      </el-table-column>
      <el-table-column prop="remark" align="center" label="备注" width="220"></el-table-column>
      <el-table-column 
          align="center"
          prop="operation"
          fiexd="right"
          label="操作"
          v-if="info == 2"
          >
        <template slot-scope="scope">
          <el-button
            type="warning"
            size="small"
            icon="edit"
            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button
            size="small"
            type="danger"
            icon="delete"
            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <DiaLog :dialogs="dialogs" :formData="formData" @updata="getProfile"></DiaLog>
  </div>
  <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page.currentPage"
      :page-sizes="page.pageSizes"
      :page-size="page.pageSize"
      :layout="page.layout"
      :total="page.total">
    </el-pagination>
  </div>
</template>

<script>
import DiaLog from '../components/DiaLog';
export default {
  data() {
    return {
      page:{
        currentPage:1,
        pageSizes:[5, 10, 15, 20],
        pageSize:5,
        layout:"total, sizes, prev, pager, next, jumper",
        total:0
      },
      tableData: [],
      rowData:[],
      filterData:[],
      time1:'',
      time2:'',
      dialogs:{
        show:false,
        title:"",
        option:""
      }, formData: {
        type: "",
        describe: "",
        income: "",
        expend: "",
        cash: "",
        remark: ""
      }    
    };
  },
  components:{
    DiaLog
  },
  computed:{
    info(){
      return this.$store.getters.user.identity;
    }
  },
  created() {
    this.getProfile();
  },
  methods: {
    //查询
    getProfile() {
      this.$axios
        .get("/api/profile/queryAll")
        .then(result => {
          //this.tableData = result.data.data;
          this.rowData = result.data.data;
          this.filterData = result.data.data;
          //设置分页数据
          this.setRow();
        })
        .catch(err => {});
    },
    //设置分页
    setRow(){
      //分页属性设置
      this.page.total =  this.rowData.length
      this.page.currentPage =1
      this.page.pageSize = 5
      //设置默认分页数据
      this.tableData = this.rowData.filter((item,index)=>{
        return index < this.page.pageSize
      });
    },
    //修改
    handleEdit(index,row){
      this.formData = {
        type: row.type,
        describe: row.describe,
        income: row.income,
        expend: row.expend,
        cash: row.cash,
        remark: row.remark,
        id:row._id
      }
      this.dialogs = {
        title:"修改资金信息",
        option:"edit",
        show:true
      };
    },
    //删除
    handleDelete(index,row){
      this.$axios.get(`/api/profile/deleteOne/${row._id}`).then((result) => {
         this.$message(result.data.msg);
         this.getProfile();
      })
    },
    //添加
    handleAdd(){
      this.formData = {
        type: "",
        describe: "",
        income: "",
        expend: "",
        cash: "",
        remark: ""
      }
      this.dialogs = {
        title:"添加资金信息",
        option:"add",
        show:true
      };
    },
    //每页显示几条
    handleSizeChange(val){
      //切换每页显示多少条
      this.page.currentPage =1
      this.page.pageSize = val
      this.tableData = this.rowData.filter((item,index)=>{
        return index < val
      });
    },
    //当前第几页
    handleCurrentChange(val){
      //获取当前页
      let index = this.page.pageSize * (val - 1);
      //获取数据总数
      let num = this.page.pageSize * val;
      //容器
      let numData = [];
        for (let i = index; i < num; i++) {
          if(this.rowData[i]){
            numData.push(this.rowData[i])
          }
          this.tableData = numData;
        }
    },
    //筛选时间
    handleSearch(){
      if(!this.time1 || !this.time2){
        this.$message({
          type:"warning",
          message:"请选择时间区域"
        });
        this.getProfile();
        return;
      }

      const sTiem = this.time1.getTime();
      const eTime = this.time2.getTime();

      this.rowData = this.filterData.filter((val,index)=>{
          let data = new Date(val.date);
          let tiem = data.getTime();

          return tiem >= sTiem && tiem <= eTime;
      })
      this.setRow();
    }
  }
};
</script>
<style scoped>
.filContainer{
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    padding: 16px;
}
.btnRight{
  float: right;
}
</style>
